package com.sundhage.util {
	import flash.accessibility.*;
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class JSAHandler {
		private static var items:Array;
		
		private static function createSilentProps():AccessibilityProperties {
			var ap:AccessibilityProperties = new AccessibilityProperties();
			ap.silent = true;
			ap.noAutoLabeling = true;
			return ap;
		}
		
		private static function createProps(name:String, description:String, shortcut:String):AccessibilityProperties {
			var props:AccessibilityProperties = new AccessibilityProperties();
			props.name = name;
			props.description = description;
			props.noAutoLabeling = true;
			props.shortcut = shortcut;
			props.silent = false;
			return props;
		}
		
		// uppdaterar acc-props
		public static function updateProps():Boolean {
			if (Accessibility.active) {
				Accessibility.updateProperties();
				return true;
			}
			return false;
		}
		
		public static function cleanText(item:TextField):void {
			item.accessibilityProperties = createSilentProps();
		}
		// tömmer allt
		public static function cleanObject(item:*, recursive:Boolean = true):void {
			var o:DisplayObjectContainer = item as DisplayObjectContainer;
			if (o.accessibilityProperties) o.accessibilityProperties.silent = true;
			else o.accessibilityProperties = createSilentProps();
			
			//o.tabIndex = -1;
			if (recursive) {
				
				cleanObj(o);
			}
			
			//updateProps();
			
			function cleanObj(item:DisplayObjectContainer):void {
				var list:Array = new Array();
				var i:int = 0;
				// ful-lösning
				try {
					while(item.getChildAt(i)) {
						list.push(item.getChildAt(i));
						i++;
					}
				} catch (e:Error) {}
				if (item.accessibilityProperties) item.accessibilityProperties.silent = true;
				else item.accessibilityProperties = createSilentProps();
				//item.tabIndex
				for (i = 0; i<list.length; i++) {
					//trace("Cleaning...");
					if (list[i] is DisplayObjectContainer)
						cleanObj(list[i]);
					else {
						try {
							if (item.accessibilityProperties) item.accessibilityProperties.silent = true;
							else item.accessibilityProperties = createSilentProps();
						} catch (e:Error) {}
					}
				}
			}
		}
		
		
		public static function addItem(item:*, name:String, desc:String, shortcut:String=""):void {
			item.tabIndex = items.length;
			item.accessibilityProperties = createProps(name, desc, shortcut);
			items.push(item);
			//return updateProps();
		}
		
		public static function updateItem(item:*, name:String, desc:String, shortcut:String=""):void {
			if (item.accessibilityProperties && shortcut == "")
				shortcut = item.accessibilityProperties.shortcut;
			item.accessibilityProperties = createProps(name, desc, shortcut);
			//updateProps();
		}
		
		public static function removeItem(item:*):Boolean {
			var flag:Boolean = false;
			for (var i:int = 0; i<items.length; i++) {				
				if (flag) {
					items[i] = items[i+1];
					items[i].tabIndex = i;			
				}
				if (items[i] == item) {
					flag=true;
				}
			}
			if (flag) items.pop();
			cleanObject(item, false);
			//updateProps();
			return flag;
		}
		
		
		private static function cleanItems():void {
			if (items == null) return;
			for (var i:int = 0; i<items.length; i++) {
				var o:DisplayObject = items[i] as DisplayObject;
				o.accessibilityProperties.silent = true;
			}
			//updateProps();
		}
		
		public static function reset(clean:Boolean):void {
			if (clean) cleanItems();
			items = new Array();
		}
		public static function getDummySprite(w:int = 30, h:int = 20):Sprite {
			var s:Sprite = new Sprite();
			s.graphics.beginFill(0x0, 0);
			s.graphics.drawRect(0,0,w,h);
			s.graphics.endFill();
			return s;
		}
		public static function getDummyButton(callback:Function, w:int = 30, h:int = 20):Sprite {
			var s:Sprite = new Sprite();
			s.buttonMode = true;
			s.graphics.beginFill(0x0, 0);
			s.graphics.drawRect(0,0,w,h);
			s.graphics.endFill();
			s.addEventListener(MouseEvent.CLICK, callback);
			return s;
		}
		private static var stage:Stage;
		public static function init(_stage:Stage):void {
			stage = _stage;
			// todo: hotkeys osv..
		}
		
	}
}